package com.github.iot.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.github.iot.R;
import com.github.iot.entity.Button;

import java.util.List;

/**
 * 管理柜门适配器
 *
 * @Author: 梁声洪
 * @Date: 2019/12/24 22:47
 * @Version: v1.0.0
 */
public class ManageButtonAdapter extends RecyclerView.Adapter<ManageButtonAdapter.DoorViewHolder> {

    private Context context;
    private List<Button> list;
    private View inflater;

    public ManageButtonAdapter(Context context, List<Button> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public DoorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.manage_door_item, parent, false);
        DoorViewHolder viewHolder = new DoorViewHolder(inflater);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DoorViewHolder holder, int position) {
        Button button = list.get(position);
        holder.mName.setText(button.getName());
        holder.mShowcaseLayout.setVisibility(View.GONE);

        if (button.getStatus() == -1) {
            holder.mStatus.setText("已隐藏");
            holder.mStatus.setTextColor(ContextCompat.getColor(context, R.color.color_red));
        } else if (button.getStatus() == 1) {
            holder.mStatus.setText("已启用");
            holder.mStatus.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class DoorViewHolder extends RecyclerView.ViewHolder {
        AppCompatTextView mName;
        LinearLayout mShowcaseLayout;
        AppCompatTextView mStatus;

        public DoorViewHolder(View itemView) {
            super(itemView);
            mShowcaseLayout = itemView.findViewById(R.id.showcase_layout);
            mName = itemView.findViewById(R.id.tv_door_name);
            mStatus = itemView.findViewById(R.id.tv_door_status);
        }
    }

}